package cn.qingyun.gis.modules.building.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import cn.qingyun.gis.modules.building.entity.BuildingProofread;
import cn.qingyun.gis.modules.building.service.IBuildingProofreadService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: 楼宇摸排表表后台管理
 * @Version: V1.0
 */
@Api(tags = "楼宇摸排表")
@RestController
@RequestMapping("/admin/building/building-proofread")
@Slf4j
public class BuildingProofreadController extends BasicController<BuildingProofread, IBuildingProofreadService> {
    @Resource
    private IBuildingProofreadService iBuildingProofreadService;

    /**
     * 分页列表查询
     *
     * @param buildingProofread
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "楼宇摸排表表-分页列表查询")
    @ApiOperation(value = "楼宇摸排表表-分页列表查询", notes = "楼宇摸排表表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BuildingProofread buildingProofread,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BuildingProofread> queryWrapper = QueryGenerator.initQueryWrapper(buildingProofread, req.getParameterMap());
        Page<BuildingProofread> page = new Page<>(pageNo, pageSize);
        IPage<BuildingProofread> pageList = iBuildingProofreadService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param buildingProofread
     * @return
     */
    @AutoLog(value = "楼宇摸排表表-添加")
    @ApiOperation(value = "楼宇摸排表表-添加", notes = "楼宇摸排表表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BuildingProofread buildingProofread) {
        iBuildingProofreadService.save(buildingProofread);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param buildingProofread
     * @return
     */
    @AutoLog(value = "楼宇摸排表表-编辑")
    @ApiOperation(value = "楼宇摸排表表-编辑", notes = "楼宇摸排表表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody BuildingProofread buildingProofread) {
        iBuildingProofreadService.updateById(buildingProofread);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼宇摸排表表-通过id删除")
    @ApiOperation(value = "楼宇摸排表表-通过id删除", notes = "楼宇摸排表表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iBuildingProofreadService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "楼宇摸排表表-批量删除")
    @ApiOperation(value = "楼宇摸排表表-批量删除", notes = "楼宇摸排表表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iBuildingProofreadService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼宇摸排表表-通过id查询")
    @ApiOperation(value = "楼宇摸排表表-通过id查询", notes = "楼宇摸排表表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        BuildingProofread buildingProofread = iBuildingProofreadService.getById(id);
        if (buildingProofread == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(buildingProofread);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param buildingProofread
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BuildingProofread buildingProofread) {
        return super.exportXls(request, buildingProofread, BuildingProofread.class, "楼宇摸排表表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BuildingProofread.class);
    }
}
